package com.rupertjones.globalcron.common.dao;

import com.rupertjones.globalcron.common.domain.JobDescriptor;
import com.rupertjones.globalcron.common.domain.JobStore;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * <p>&copy Rupert Jones 2013</p>
 *
 * @author rup
 */
public class MemoryJobStore implements JobStore {

    private volatile Collection<JobDescriptor> jobDescriptors = Collections.synchronizedList(new ArrayList<JobDescriptor>());

    @Override
    public Collection<JobDescriptor> getAllJobs() {
        return jobDescriptors;
    }

    @Override
    public void upsert(JobDescriptor job) {
        jobDescriptors.add(job);
    }

    @Override
    public JobDescriptor findById(int id) {
        for (JobDescriptor job : jobDescriptors) {
            if (job.getId() == id) {
                return job;
            }
        }
        return null;
    }
}
